import { IColumnProps } from "devextreme-react/data-grid";
import { getIniTypedSampleInfo, getTypedSampleList } from "./component/samplingSampleListService";
import { consts } from "./consts";
import { getSampleRecordList } from "./tableSamplingRecordService";
import { SampleReord, SampleTable, TypedSampleInfo } from "./typing";
/**
 * 获取分类样本
 * @param dbname 
 * @param sampleTable 
 * @returns 
 */
export async function getTypedSampleInfoOfSampleTable(dbname: string, sampleTable: SampleTable)
    : Promise<TypedSampleInfo> {

    const sampleRecordList: SampleReord[] = await getSampleRecordList(dbname,
        sampleTable.tableTitle, '', 0, consts.maxSampleCount);

    const allTypedSampleInfo = getIniTypedSampleInfo();
    for (let i = 0; i < sampleRecordList.length; i++) {
        const sampleReord = sampleRecordList[i];
        if (typeof (sampleReord.sampleStepInfo) == 'object'
            && sampleReord.sampleStepInfo?.sampleParameterValues) {
            const typedSampleInfo = await getTypedSampleList(sampleReord.sampleStepInfo,
                sampleReord.sampleStepInfo.sampleParameterValues, false);

            //把单个的增加到总的里
            allTypedSampleInfo.specialSampleList.push(...typedSampleInfo.specialSampleList);//特殊样本
            allTypedSampleInfo.amountMajorSampleList.push(...typedSampleInfo.amountMajorSampleList);//: [],// 金额重大样本列表
            allTypedSampleInfo.manualAddSampleList.push(...typedSampleInfo.manualAddSampleList);//: [],// 手动添加样本列表

            allTypedSampleInfo.specialSampleTextFieldValues.push(...typedSampleInfo.specialSampleTextFieldValues);//: [],//特殊样本
            allTypedSampleInfo.amountMajorSampleTextFieldValues.push(...typedSampleInfo.amountMajorSampleTextFieldValues);//: [],// 金额重大样本列表
            allTypedSampleInfo.manualAddSampleTextFieldValues.push(...typedSampleInfo.manualAddSampleTextFieldValues);//: [],// 手动添加样本列表
            //sampleReord.typedSampleInfo = typedSampleInfo;
        }

    }
    return allTypedSampleInfo;
}
export function getColumnYesNoEditLookup() {
    return {
        dataSource: consts.AcYesNoOnlyListOfEmpty,
        keyExpr: 'id',
        valueExpr: 'id',
        displayExpr: 'text'
    }
}
export function canEditHeaderCellTemplate(cellElement: HTMLElement, cellData: any) {
    let text = cellData.column.caption;
    cellElement.innerHTML = `<span  title="${text} ` + '此列导出后可编辑'
        + `" style="color:var(--cpasPrimaryColor);">${text}</span>`
}
/**
 * 增加错包的列
 * @returns 
 */
export function getErrorColumns(): IColumnProps[] {
    return [
        {
            dataField: 'isError',
            caption: '是否错报',
            width: 90,
            alignment: 'center',
            allowEditing: true,
            headerCellTemplate: canEditHeaderCellTemplate,

            allowSorting: false,
            allowFiltering: false,
        },
        {
            dataField: 'errrorAmount',
            caption: '错报金额',
            width: 100,
            alignment: 'right',
            format: consts.numberFormat,
            allowEditing: true,
            dataType: 'number',
            headerCellTemplate: canEditHeaderCellTemplate,
            allowSorting: false,
            allowFiltering: false,

        },
        {
            dataField: 'errrorMemo',
            caption: '备注',
            width: 120,
            alignment: 'left',
            allowEditing: true,
            headerCellTemplate: canEditHeaderCellTemplate,
            allowSorting: false,
            allowFiltering: false,
        },
        {
            dataField: 'wpIndex',
            caption: '索引号',
            width: 120,
            alignment: 'left',
            allowEditing: true,
            headerCellTemplate: canEditHeaderCellTemplate,
            allowSorting: false,
            allowFiltering: false,
        }]
}